package com.apes.framework.plugin.form.parser.element;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.filereader.xml.ApesElementParser;
import com.apes.framework.plugin.form.parser.matcher.AttrsMatcher;
import com.apes.framework.plugin.form.parser.matcher.DomainMatcher;
import com.apes.framework.plugin.form.parser.matcher.ExpressionMatcher;
import com.apes.framework.plugin.form.parser.node.FormNodeParser;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 功能：table解析
 *
 * @author xul
 * @create 2018-04-16 16:12
 */
@Component
public class TableElementParser implements ApesElementParser<ApesElement, Map> {

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"table"};

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return FormNodeParser.domain;
    }

    /**
     * 功能：可解析的元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }

    @Override
    public Map parse(ApesElement element) {
        Map node = new HashMap();
        if (!StringUtils.isEmpty(element.getText())){
            node.put("widgets",element.getText());
            JSONObject json = (JSONObject) JSON.parse(node.get("widgets").toString());
            node.putAll(json);
        }
        parseAttributes(node, element);
        parseNestedElements(node, element);
        return node;
    }


    protected void parseAttributes(Map node, ApesElement element) {
        Map expressionProperties = new HashMap();
        Map templateOptions = new HashMap();
        element.getAttributes().forEach((k, v) -> {
            if (k.equals("name")) {
                node.put("key", v.trim());
                node.put("tableValid", v.trim());
            } else if (k.equals("attrs")) {
                Map<String, String> attrs = new AttrsMatcher().parse(v.trim());
                attrs.forEach((k1, v1) -> {
                    v1 = v1.replace(ExpressionMatcher.getPlaceholder(), "formState.");
                    if (k1.equals("hide")) {
                        node.put("hideExpression", v1);
                    } else {
                        expressionProperties.put("templateOptions." + k1, v1);
                    }
                });
            } else if (k.equals("hide")) {
                node.put("hideExpression", v.replaceAll("ctx.", "formState."));
            } else if (k.equals("disabled") || k.equals("required")) {
                expressionProperties.put("templateOptions." + k, v.replaceAll("ctx.", "formState."));
            } else if (k.equals("btnhide") || k.equals("source") || k.equals("import")) {
                templateOptions.put(k, v);
               // node.put("templateOptions", m);
            } else if (k.equals("divCol") || k.equals("lblCol") || k.equals("inputCol")) {
               // Map m = new HashMap();
                templateOptions.put(k, Integer.valueOf(v));
                //node.put("templateOptions", m);
            } else if (k.equals("domain")) {
                node.put(k.trim(), new DomainMatcher().parse(v.trim()));
            } else{
                node.put(k.trim(), v.trim());
            }
        });
        node.put("expressionProperties", expressionProperties);
        node.put("templateOptions", templateOptions);
        node.put("type", "grid");
        if (node.containsKey("widget")){
            node.put("type",node.get("widget"));
        }
    }

    protected void parseNestedElements(Map node, ApesElement element) {
//        Map index = new HashMap();
//        index.put("headerName", "序号");
//        index.put("field", "rowIndex");
        List columnDefs = element.getNestedElements().stream()
                .map(apesElement -> apesElement.parse()).collect(Collectors.toList());
//        columnDefs.add(0, index);
        Map m = node.containsKey("templateOptions") ? (Map) node.get("templateOptions") : new HashMap();
        m.put("columnDefs", columnDefs);
        node.put("templateOptions", m);

    }
}
